<?php

class CustomerController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action admroller here */
        $this->list_sess_unset('controller_customer');
        $this->session_search_key = 'controller_customer';
        $this->session_search_name = 'search_label';
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'customer', '');
    }

    public function viewAction() {
        if ($this->_getParam('cus_id') && $this->_getParam('cus_id') != "") {
            $cus_id = $this->_getParam('cus_id');
        } else {
            $this->_helper->redirector('list', 'customer', '');
        }
        $customerModel = new Model_Customer();
        $customeralldata = $customerModel->fetchEntryByViewId($cus_id);
        $this->view->customeralldata = $customeralldata;
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $orderField = 'cus_id';
        $sort = 'asc';
        if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) {
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = "";
        }


        /* Pagination Login */

        $itemsPerPageReviews = $this->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        //PAGING_PER_PAGE
        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }
        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();
            //Multiple Delete Logic 
            if (isset($postedData["multiaction"]) && ($postedData["multiaction"] != "")) {
                $action = trim($postedData["multiaction"]);
                if (isset($postedData["multicheck"]) && count($postedData["multicheck"]) > 0) {
                    
                    $ids = implode(",", $_POST["multicheck"]);
                    $result = $this->deleteAction($_POST["multicheck"]);
                } else {
                    $errorMessage[] = ' Please select atleast one checkbox! ';
                }
            }
            /* Search Facility Over Here */
            if ($postedData["searchtype"] == "") {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = "";
            }
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] != "")) {
                $searchtype = $postedData["searchtype"];
                $where = "cus_name LIKE '%$searchtype%' OR cus_actnumber LIKE '%$searchtype%' ";
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                $this->view->searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
            }
        } else {

            if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
            }
            $searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
            $where = "cus_name LIKE '%$searchtype%' OR cus_actnumber LIKE '%$searchtype%' ";
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
            $this->view->searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
        }

        $customerModel = new Model_Customer();
        $customersData = $customerModel->fetchCustomer($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($customerModel->getCustomerCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'customer', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->customersData = $customersData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->statusArray = $customerModel->getStatusArray();
        $this->view->searchtype = $searchtype;
    }

    public function deleteAction($ids = Array()) {
        //Load Customer Model 
        
        $customerModel = new Model_Customer();
       
       // $customerData = $customerModel->fetchEntryById($cus_id);
        if ($this->_getParam('cus_id') && $this->_getParam('cus_id') != "") {
            $cus_id = $this->_getParam('cus_id');
            $customerModel->deleteCustomer($cus_id);
            $this->addSuccessMessage('Record Successfully Deleted');
            $this->_helper->redirector('list', 'customer', '');
        } else {
            $customerModel->deleteCustomer($ids);
            $this->addSuccessMessage('Record Successfully Deleted');
            $this->_helper->redirector('list', 'customer', '');
        }
    }

    public function formAction() {

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $customerData = Array();
        $this->view->mode = "Add";

        //Load Customer Model
        $customerModel = new Model_Customer();

        //Load Customer Form
        $customerForm = new Form_CustomerForm();

        //Load the status array for the select box in form 
        $statusArray = $customerModel->getStatusArray();
        $customerForm->cus_status->addMultiOptions($statusArray);




        //Check for the customer Parameter
        $customerData['cus_id'] = $this->_request->getParam('cus_id');
        $cus_id = (!empty($customerData['cus_id'])) ? $customerData['cus_id'] : null;

        if (!empty($cus_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $customerModel->fetchEntryById($cus_id);
        }
        //Populate form 
        $this->view->form = $customerForm->populate($data);
        if ($this->getRequest()->isPost()) {
            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$customerForm->isValid($postedData)) {
                $errorMessage = $customerForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if (count($errorMessageArray) > 0) {
                    
                } else {
                    if ($this->view->mode == "Add") {
                        $customerData['cus_createddate'] = date("Y-m-d H:i:s");
                    }
                    //Customer save
                    $customerData['cus_id'] = $cus_id;
                    $customerData['cus_name'] = $postedData['cus_name'];
                    $customerData['cus_actnumber'] = $postedData['cus_actnumber'];
                    $customerData['cus_mobile'] = $postedData['cus_mobile'];
                    $customerData['cus_address'] = $postedData['cus_address'];
                    $customerData['cus_note'] = $postedData['cus_note'];
                    $customerData['cus_status'] = $postedData['cus_status'];
                    $customerData['cus_updateddate'] = date("Y-m-d H:i:s");
                    $customerModel->save($customerData, 'cus_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = 'Customer add successfully';
                    } else {
                        $successmessage = 'Customer updated successfully';
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'customer', '');
                }
            }
        }
        $this->view->customerid = $cus_id;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $customerForm;
    }

}

?>
